package com.ruoyi.supermarket.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.supermarket.bean.OrderItemRes;
import com.ruoyi.supermarket.bean.OrderRecordRes;
import com.ruoyi.supermarket.domain.OrderRecord;
import com.ruoyi.supermarket.service.OrderRecordService;
import com.ruoyi.supermarket.service.ShopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supermarket.domain.OrderItem;
import com.ruoyi.supermarket.service.OrderItemService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单细则Controller
 * 
 * @author 李其伟
 * @date 2023-11-14
 */
@RestController
@RequestMapping("/supermarket/orderItem")
public class OrderItemController extends BaseController
{
    @Autowired
    private OrderRecordService orderRecordService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private ShopService shopService;
    /**
     * 查询订单细则列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderItem:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderItem orderItem)
    {
        //店铺用户只能查询自己的数据
        if (hasShop()) {
            orderItem.setShopId(shopService.selectShopByUserId(getUserId()));
        }
        startPage();
        List<OrderItem> list = orderItemService.selectOrderItemList(orderItem);
        TableDataInfo tableDataInfo = getDataTable(list);

        List<OrderItem> orderItemList = (List<OrderItem>) tableDataInfo.getRows();
        if(orderItemList.size()==0){
            return tableDataInfo;
        }

        String[] orderSns = orderItemList.stream().map(OrderItem::getOrderSn).toArray(String[]::new);
        List<OrderRecord> orderRecordList = orderRecordService.selectOrderRecordByOrderSn(orderSns);
        Map<String,OrderRecord> orderRecordMap = orderRecordList.stream().collect(Collectors.toMap(OrderRecord::getOrderSn, Function.identity()));

        List<OrderItemRes> orderItemResList = new ArrayList<>();
        for(OrderItem oi : orderItemList){
            OrderItemRes oir = new OrderItemRes();
            oir.setOrderItem(oi);
            oir.setOrderRecord(orderRecordMap.get(oi.getOrderSn()));
            orderItemResList.add(oir);
        }
        tableDataInfo.setRows(orderItemResList);
        return tableDataInfo;
    }

    /**
     * 导出订单细则列表
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderItem:export')")
    @Log(title = "订单细则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderItem orderItem)
    {
        List<OrderItem> list = orderItemService.selectOrderItemList(orderItem);
        ExcelUtil<OrderItem> util = new ExcelUtil<OrderItem>(OrderItem.class);
        util.exportExcel(response, list, "订单细则数据");
    }

    /**
     * 获取订单细则详细信息
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderItem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderItemService.selectOrderItemById(id));
    }

    /**
     * 新增订单细则
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderItem:add')")
    @Log(title = "订单细则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderItem orderItem)
    {
        return toAjax(orderItemService.insertOrderItem(orderItem));
    }

    /**
     * 修改订单细则
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderItem:edit')")
    @Log(title = "订单细则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderItem orderItem)
    {
        return toAjax(orderItemService.updateOrderItem(orderItem));
    }

    /**
     * 删除订单细则
     */
    @PreAuthorize("@ss.hasPermi('supermarket:orderItem:remove')")
    @Log(title = "订单细则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderItemService.deleteOrderItemByIds(ids));
    }
}
